﻿define(['text!/cms/html/article-type-index.html'], function (template) {
    new Vue({
        template: template,

        el: '#article-type-index',

        data: {
        },

        methods: {
            getArticleTypes: function () {
                var vm = this;
                $.post('/cms/articleType', {}).done(function (res) {
                    _.each(res.result, function (item) {
                        item.parent = item.parentId;
                        item.text = item.title;
                        if (!item.parent) item.parent = '#';
                    });
                    vm.jstreeInit(res.result)
                });
            },
            jstreeInit: function (data) {
                $('#tree').jstree({
                    plugins: ['contextmenu'],
                    contextmenu: {
                        items: {
                            add: {
                                label: "编辑",
                                action: function (obj) {
                                    var inst = jQuery.jstree.reference(obj.reference);
                                    var clickedNode = inst.get_node(obj.reference);
                                    location.href= "/cms/articleType/update/"+clickedNode.id;
                                    var newNode = inst.create_node(inst.get_node(obj.reference), '请输入分类名称', "after", "", "");
                                    inst.edit(newNode, newNode.val)
                                }
                            }
                        }
                    },
                    core: {
                        data: data,
                    }
                });
            }
        },

        created: function () {
            this.getArticleTypes();
        }
    })

});
